<?php 
namespace app\mall\model;
use think\Model;
use think\Db;
class Goods extends Model{
	protected $pk = 'id';
	protected $table = 'ys_goods';
    protected $field = true;        //自动忽略不存在的字段
	protected $validate = [
		['name', 'require|max:50', '商品名称不能为空|商品名称最多50个字符'],
		['subtitle', 'require|max:50', '副标题不能为空|分标题标题最多50个字符'],
		['taste', 'require|max:20', '口味不能为空|口味最多20个字符'],
		['save_method', 'require|max:50', '保存方法不能为空|保存方法最多20个字符'],
        ['eat_num', 'require|max:50', '分享人数不能为空|分享人数最多20个字符'],
        ['good_number', 'require|max:20|unique:goods', '商品货号不能为空|商品货号最多20位|商品货号已被占用，请重新输入'],
        ['cat_id', 'require', '商品分类不能为空'],
        ['exp_cat', 'require', '商品扩展分类不能为空'],
        ['base_spec', 'require', '请输入商品基本规格'],
        ['unit', 'require', '请输入商品单位'],
//        ['price', 'require|/^([0-9][\d]{0,7})(\.[\d]{1,2})?$/', '商品价格不能为空|商品价格输入错误'],
        ['price', 'require', '商品价格不能为空'],
        ['member_level_id', 'require', '请选择会员商品类型'],
        ['is_online', 'require', '请选择是否允许线下销售'],
        ['stock', 'require|/^([0-9][\d]{0,7})$/', '请输入商品的默认库存|商品的默认库存只能输入整数'],
        ['status', 'require', '请选择是否上架'],
        ['description', 'require|max:100', '商品描述不能为空|商品描述最多不能超过100个字符'],
        ['detail', 'require', '商品详情不能为空']
	];
	protected function initialize(){
        parent::initialize();
    }

    /**
     * 商品添加 -- 编辑
     */
    public function add_edit($data){
        try{
            if(isset($data['id']) && $data['id']>0){        //更新
                $result = $this->validate($this->validate)->save($data,['id',$data['id']]);
                if(false === $result){
                    return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
                }else{
                    return ['code' => 1, 'data' => '', 'msg' => '编辑成功','goods_id'=>$data['id']];
                }
            }else{      //新增
                $result = $this->validate($this->validate)->save($data);
                if(!$result){
                    return ['code'=>0,'msg'=>$this->getError()];
                }else{
                    return ['code'=>1,'msg'=>'添加成功！','goods_id'=>$this->id];
                }
            }
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 设置状态
     * @return [type] [description]
     */
    public function setstatus($data,$id){
    	try{
    		$result = $this->validate(false)->save($data,['id'=>$data['id']]);
    		if(false === $result){
                return ['code' => 0, 'data' => '', 'msg' => '设置失败'];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '设置成功'];
            }
    	}catch(PDOException $e){
    		return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
    	}
    }
    /**
     * 删除门店
     * @param  id   array   删除的商品ID数组
     * @return [type] [description]
     */
    public function del($id){
        $result = Db::name('store_goods')->where('id','in',$id)->limit(1)->find();
        if(!empty($result)){
            return ['code'=>'0','data'=>'','msg'=>'该商品已在门店中出售，不能删除'];
        }
        $res = $this->where('id','id',$id)->save(array('is_delete'=>1));
        if($res!==false){
            return ['code'=>'1','data'=>'','msg'=>'加入回收站成功'];
        }else{
            return ['code'=>'0','data'=>'','msg'=>'加入回收站失败'];
        }
    }
    /**
     * 成品购货申请处理成品的库存
     * @return [type] [description]
     */
    public function save_order($da){
        $datas = $this->update_order($da);       //处理数
        $result = $this->saveall($datas);          //保存所有数据
        if($result!==false){
            return ['code'=>1,'mag'=>'保存成功'];
        }else{
            return ['code'=>0,'msg'=>'保存失败'];
        }
    }
    public function update_order($da){
        $good= $da['material_id'];
        $del_num = $da['del_num'];
        $stock = $da['stock'];      //原料库存
        $materlial_type = $da['material_type'];
        foreach($good as $k => $v){
            $data[$k]['id'] = $v;
            $data[$k]['stock'] = $stock[$k]-$del_num[$k];
        }
        return $data;
    }

    /**
     * @Author  lingyun
     * @Desc    推荐商品
     * @Param   id  推荐商品ID
     * 2018-03-24
     */
    public function set_top($id){
        $info = $this->where('id',$id)->field('id,name,is_top')->find();
        $status = empty($info['is_top'])?time():0;
        $result = $this->validate(false)->save(['is_top'=>$status],['id'=>$id]);
        if($result!==false){
            return ['code'=>1,'msg'=>'推荐成功'];
        }else{
            return ['code'=>0,'msg'=>'推荐失败'];
        }
    }
    
}
